<?php
include_once '../../lib/config.php';
include_once '../../lib/db.php';
include '../../lib/utils/date.php';
include '../../lib/utils/tag.php';
include '../../model/departemen.php';
include '../../model/karyawan.php';
include '../../model/rangking.php';
include '../../model/perbuatan.php';
include '../../model/report.php';
include '../../model/report2.php';

$proc = $_REQUEST['proc'];

switch ($proc):
case 'graph':
	include ("../../lib/jpgraph/jpgraph.php");
	include ("../../lib/jpgraph/jpgraph_bar.php");
	
	//generate data
	$startPeriode = $_REQUEST['periode1'];
	$endPeriode = $_REQUEST['periode2'];
	$deptID = $_REQUEST['departemenID'];
	if (!isset($deptID) || !$startPeriode || !$endPeriode) { return; }
	
	$deptID = is_array($deptID)? $deptID : array($deptID);
	
	$graphData = array();
	foreach($deptID as $dd){
		$ddd = mysql_fetch_assoc(departemen_load($dd));
		$graphData ['label'][] = $ddd['NAMA_DEPARTMENT'];
	}
	
	$CSIM = array();
	$depc=0;
	foreach ( rep_countRewardPunishment($deptID, $startPeriode, $endPeriode ) as $tmp){
		$graphData['data']['pr'][] = $tmp['prestasi'];
		$CSIM['pr']['target'][] = "load_drillGraph(".json_encode(array(
				"type"=> '1',
				'deptID'=> $deptID[$depc],
				'startPeriode'=> $startPeriode,
				'endPeriode'=> $endPeriode,
			)).")";
		$CSIM['pr']['alt'][] = "%0.1f";

		$graphData['data']['pe'][] = $tmp['pelanggaran'];
		$CSIM['pe']['target'][] = "load_drillGraph(".json_encode(array(
				"type"=> '-1',
				'deptID'=> $deptID[$depc],
				'startPeriode'=> $startPeriode,
				'endPeriode'=> $endPeriode,
			)).")";
		$CSIM['pe']['alt'][] = "%0.1f";
		
		$depc++;
	}

	//Create the bar plot	
	$bplot1 = new BarPlot($graphData['data']['pr']);
	$bplot1->SetFillColor("blue");
	$bplot1->SetWidth(0.5);
	$bplot1->SetLegend('Rata-rata Reward');
	$bplot1->SetCSIMTargets($CSIM['pr']['target'],$CSIM['pr']['alt']);
	$bplot1->value->Show();
	
	$bplot2 = new BarPlot($graphData['data']['pe']);
	$bplot2-> SetFillColor("red");
	$bplot2->SetWidth(0.5);
	$bplot2->SetLegend('Rata-rata Punishment');
	$bplot2->SetCSIMTargets($CSIM['pe']['target'],$CSIM['pe']['alt']);
	$bplot2->value->Show();

	// Create the graph.
	$graph = new Graph(700,500,"auto");
	$graph->SetFrame(false,'white@.5',1); 
	$graph->title->Set("Perolehan Reward Departemen per Departemen");
	$graph->SetColor("white"); 
	$graph->SetScale("textlin");
	$graph->img->SetMargin(80,30,40,80);
	$graph->img->SetAntiAliasing(); 
	
	$graph->ygrid->SetFill(true,'#FFFFFF@0.5','#d7e5f5@0.5');
	$graph->yaxis->SetFont(FF_FONT1,FS_BOLD);
	$graph->yaxis->SetPos("min");
	$graph->yaxis->title->SetFont(FF_FONT1,FS_BOLD);
	$graph->yaxis->title->set("Rata - rata");
	$graph->yaxis->SetTitleMargin(50); 
	
	$graph->xaxis->SetFont(FF_FONT1,FS_BOLD);
	$graph->xaxis->SetPos("min");
	$graph->xaxis->SetTickLabels($graphData['label']);
	$graph->xaxis->title->SetFont(FF_FONT1,FS_BOLD);
	$graph->xaxis->title->set("Departemen");
	if ( count($graphData['label'])>10 ) {
		$graph ->xaxis->SetTextLabelInterval(count($graphData['label'])/4);
	}
	
	$graph->legend->SetLayout(LEGEND_HOR);
	$graph->legend->Pos(0.4,0.95,"center","bottom");
	
	// Add the plot to the graph
	$graph->Add(new GroupBarPlot(array($bplot1 ,$bplot2)));
	
	// Display the graph
	$graph->StrokeCSIM('proc/admin/perolehanRP.php');
	break;
	
case 'graph-drill-reward':
	$deptID = $_REQUEST['deptID'];
	$jenis = $_REQUEST['jenis'];
	$startPeriode = $_REQUEST['startPeriode'];
	$endPeriode = $_REQUEST['endPeriode'];
	include '../../view/admin/perolehanRP/drill-reward.php';
	break;
endswitch;